package cn.study.controller;


import cn.study.entity.StudyDepartment;
import cn.study.entity.StudyEmployee;
import cn.study.entity.StudyRole;
import cn.study.service.StudyDepartmentService;
import cn.study.service.StudyEmployeeService;
import cn.study.service.StudyRoleService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 员工表 前端控制器
 * </p>
 *
 * @author qiao
 * @since 2020-06-22
 */
@Controller
@Slf4j
@RequestMapping("/study-employee")
public class StudyEmployeeController {
    @Autowired
    StudyEmployeeService studyEmployeeService;
    @Autowired
    StudyDepartmentService studyDepartmentService;
    @Autowired
    StudyRoleService studyRoleService;

    @GetMapping("/list")
    public String emplist(Model model,@RequestParam(value="pn",defaultValue="1") Integer pn,
                                    @RequestParam(value="paginalsize",defaultValue = "5") Integer paginalsize
    ){
        PageHelper.startPage(pn, paginalsize);
        List<StudyEmployee> emplist=studyEmployeeService.getByDepByRoleList();
        PageInfo pInfo=new PageInfo(emplist,10);
        model.addAttribute("pInfo",pInfo);
        model.addAttribute("paginalsize",paginalsize);
        return "study_employee";
    }
    @GetMapping("/get/empadd")
    public String getempadd(Model model){
        List<StudyDepartment> deplist=studyDepartmentService.list();
        List<StudyRole> rolelist=studyRoleService.list();
        model.addAttribute("deplist",deplist);
        model.addAttribute("rolelist",rolelist);
        return "study_employee_add";
    }
    @PostMapping("/empadd")
    public String empadd(StudyEmployee studyEmployee){
        boolean bl=studyEmployeeService.save(studyEmployee);
        if(bl){
            log.info("添加成功");
        }else {
            log.info("添加失败");
        }
        return "redirect:/study-employee/list";
    }
    @GetMapping("/get/empmodification/{id}")
    public String getempmodification(Model model, @PathVariable("id") String id){
        model.addAttribute("studyemp",studyEmployeeService.getById(id));
        model.addAttribute("deplist",studyDepartmentService.list());
        model.addAttribute("rolelist",studyRoleService.list());
        return "study_employee_upd";
    }
    @PostMapping("/empmodification")
    public String empmodification(StudyEmployee studyEmployee){
        studyEmployeeService.updateById(studyEmployee);
        return "redirect:/study-employee/list";
    }

}

